//
//  JXMyMarkController.m
//  JiaoX
//
//  Created by yinqy on 15/1/21.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//  我的收藏

#import "JXMyMarkController.h"
#import "SelfDataTool.h"
#import "HomeProdViewFrame.h"
#import "ProductItemInfo.h"
#import "HomeProdItemCell.h"
#import "JXProdDetailCtl.h"
#import "DeleteBottomView.h"
#import "HomeDataTool.h"
@interface JXMyMarkController()<DeleteBottomViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_productFrames;
}
@property (nonatomic,strong)DeleteBottomView *bottomView;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation JXMyMarkController

-(void)viewDidLoad{
    [super viewDidLoad];
    _productFrames  = [NSMutableArray array];
    [self setTitle:@"我的收藏"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.allowsSelectionDuringEditing = YES;
    [self.tableView setTableFooterView:[[UIView alloc]init]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self requestData];
}


-(void)requestData{
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.navigationController.view];
    [SelfDataTool getCollectList:^(ProductSearch *result) {
        [HUD hide:YES];
        if ([Utils isBlankString:result.data]) {
            //加载成功
            [self loadDatas:result];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
    }];
}

-(void)loadDatas:(ProductSearch *)result{
    [_productFrames removeAllObjects];
    for (ProductItemInfo *prod in result.productList) {
        HomeProdViewFrame *frame = [[HomeProdViewFrame alloc]init];
        frame.product = prod;
        [_productFrames addObject:frame];
    }
    [self.tableView reloadData];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_productFrames.count){
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }else{
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _productFrames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeProdItemCell *cell = [HomeProdItemCell cellWithTableView:tableView];
    cell.homeProdFrame = _productFrames[indexPath.row];
    [cell.homeProdView hideSpliteLine:YES];
    [cell setChecked:cell.homeProdFrame.product.checked];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeProdViewFrame *frame = _productFrames[indexPath.row];
    return frame.cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeProdViewFrame *frame = _productFrames[indexPath.row];
    if (!self.tableView.editing)
    {
        JXProdDetailCtl *proddc = [[JXProdDetailCtl alloc]initWithPordId:[NSString stringWithFormat:@"%d",frame.product.prodId]];
        [self.navigationController pushViewController:proddc animated:YES];
    }else{
        
        HomeProdItemCell *cell = (HomeProdItemCell*)[tableView cellForRowAtIndexPath:indexPath];
        
        frame.product.checked = !frame.product.checked;
        [cell setChecked:frame.product.checked];
        [self.bottomView setSelect:[self isSelectAll]];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}



-(BOOL)shouldLogin{
    return YES;
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsMake(0, JXPublicInset, 0, 0)];
}


-(void)edit{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
    if (self.tableView.editing) {
        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
        //是编辑模式
        if (!self.bottomView) {
            self.bottomView = [[DeleteBottomView alloc]init];
            self.bottomView.y = self.view.height-self.bottomView.height;
            [self.bottomView setDelegate:self];
            [self.view addSubview:self.bottomView];
        }
        [self.bottomView setHidden:NO];
        [self.tableView setHeight:self.view.height-49];
    }else{
        [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
        //不是编辑模式
        [self.bottomView setHidden:YES];
        [self.tableView setHeight:self.view.height];
    }
}

-(void)selectAllBtnClick:(UIButton *)btn{
    if (btn.selected) {
        //全选
        [_productFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            HomeProdViewFrame *frame = obj;
            [frame.product setChecked:YES];
        }];
    }else{
        //取消全选
        [_productFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            HomeProdViewFrame *frame = obj;
            [frame.product setChecked:NO];
        }];
    }
    [self.tableView reloadData];
}

-(void)deleteBtnClick{
    //删除选中的条目
    NSMutableString *mutableString = [NSMutableString string];
    
    for (HomeProdViewFrame *frame in _productFrames) {
        if (frame.product.checked) {
            [mutableString appendFormat:@"%d,",frame.product.prodId];
        }
    }
    NSString *result = nil;
    if (mutableString.length>0) {
        result = [mutableString substringWithRange:NSMakeRange(0, mutableString.length-1)];
    }
    
    if (result) {
        //去取消收藏
        [HomeDataTool delMarkProdWithProdIds:result success:^(JXBaseResponse *result) {
            if (ISBLANK(result.data)) {
                //操作成功,遍历
                NSMutableArray *mutableArray = [NSMutableArray array];
                for (HomeProdViewFrame *frame in _productFrames) {
                    if (frame.product.checked) {
                        [mutableArray addObject:frame];
                    }
                }
                [_productFrames removeObjectsInArray:mutableArray];
                [self edit];
                [self.tableView reloadData];
            }else{
                [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
            }
        } failure:^(NSError *error) {
            [DialogUtils showTipsWithView:self.navigationController.view String:@"操作失败"];
        }];
    }
}

-(BOOL)isSelectAll{
    BOOL result = YES;
    for (HomeProdViewFrame *frame in _productFrames) {
        if (!frame.product.checked) {
            return NO;
        }
    }
    return result;
}

@end
